package com.kye.express.business.freight_rates_query;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatDialogFragment;
import com.kye.express.utils.PhoneUtils;
import com.kye.express.view.KyeProgressDialog;
import com.kye.express.view.ToastManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.freight_rates_query.PermissionQueryCostResultBean;
import com.kye.model.bean.freight_rates_query.QueryFreightRightsResultBean;

import io.reactivex.functions.Consumer;

/**
 * 没有权限进入月结运费查询，拨打市场专员dialog
 */
public class FreightRatesDialogForCallFragment extends BaseAppCompatDialogFragment implements FreightRatesDialogFragmentContract.FreightRatesDialogFragmentView {

    private static final String ARG_DATA = "arg_data";
    private PermissionQueryCostResultBean mPermissionQueryCostResultBean;

    private FreightRatesDialogFragmentContract.FreightRatesDialogFragmentViewPresenter mPresenter;

    private Dialog mLoadingDialog;

    public static FreightRatesDialogForCallFragment newInstance(PermissionQueryCostResultBean permissionQueryCostResultBean) {
        FreightRatesDialogForCallFragment fragment = new FreightRatesDialogForCallFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(ARG_DATA, permissionQueryCostResultBean);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Context context = getActivity();
        mPresenter = new FreightRatesDialogFragmentContract.FreightRatesDialogFragmentViewPresenter(getActivity());
        mPresenter.bind(this);
        Bundle bundle = getArguments();
        if (bundle == null) {
            dismiss();
        }
        mPermissionQueryCostResultBean = (PermissionQueryCostResultBean) bundle.getSerializable(ARG_DATA);

        View view = View.inflate(context, R.layout.dialog_freight_rates_dialog_for_call, null);
        Button callButton = view.findViewById(R.id.btn_call);
        TextView cancelButton = view.findViewById(R.id.tv_cancel);
        TextView okButton = view.findViewById(R.id.tv_ok);
        EditText codeView = view.findViewById(R.id.edt_intput_code);
        RxView.clicks(callButton)
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        if (mPermissionQueryCostResultBean != null && (!TextUtils.isEmpty(mPermissionQueryCostResultBean.getPhone()))) {
                            PhoneUtils.call(getActivity(), mPermissionQueryCostResultBean.getPhone());
                        }
                    }
                });

        RxView.clicks(cancelButton)
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        dismiss();
                    }
                });
        RxView.clicks(okButton)
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        String code = codeView.getText().toString().trim();
                        if (!TextUtils.isEmpty(code)) {
                            mPresenter.queryFreightRights(code);
                        } else {
                            ToastManager.show(getActivity(), R.string.frq_code_is_null, Toast.LENGTH_SHORT);
                        }

                    }
                });


        final AlertDialog dialog = new AlertDialog.Builder(context)
                .setView(view)
                .setInverseBackgroundForced(true)
                .create();
        return dialog;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.unbind();
    }

    @Override
    public void onLoaded(OpenApiResult<QueryFreightRightsResultBean> queryCostResult) {
        MounthQueryActivity.startActivity(getActivity());
        dismiss();
    }

    @Override
    public void onComplete() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }

    @Override
    public void onError(String msg) {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
        ToastManager.show(getActivity(), msg, Toast.LENGTH_SHORT);
    }

    @Override
    public void onLoading() {
        try {
            if (mLoadingDialog == null) {
                mLoadingDialog = new KyeProgressDialog.Builder(getActivity())
                        .build();
            }
            mLoadingDialog.show();
        } catch (Exception e) {

        }
    }
}
